"use strict"

class user_login_data
{
    constructor(opcode, usertype, userid, password)
    {   
        /*
            opcode:
            login -> 1
        */
        this.opcode = opcode; 
        this.usertype = usertype; // businessman/customer/manufacturer
        this.userid = userid;
        this.password = password;
    }
}


// http://192.168.1.1:8080/server
const database_server_url = "http://192.168.1.1:8080/server";
const html_server_url = "http://127.0.0.1:8080/server"; // 用于页面跳转
var data = null;

// 定义login_button的事件属性
const login_button = document.getElementById("login_button");
// console.log(login_button);
login_button.onclick = function()
{
    // 获取用户的输入: 用户类型, 用户名, 密码
    const userid_element = document.getElementById("userid");
    const password_element = document.getElementById("password");
    const usertype_elements = document.getElementsByName("usertype");
    let selected_usertype_element = null;
    for(let i = 0; i < usertype_elements.length; ++i)
    {
        if (usertype_elements[i].checked)
        {
            selected_usertype_element = usertype_elements[i];
            break;
        }
    }

    // 预判断是否是合法输入, username, usertype, password为空, 输入有非法字符等. 暂不判断, 假定用户输入合法

    //console.log(usertype_elements, typeof usertype_element);
    //console.log(usertype_elements[0].value, usertype_elements[0].name);
    data = new user_login_data(1, selected_usertype_element.value, userid_element.value, password_element.value); // 仍然需要处理
    //console.log(typeof data.usertype);
    let data_to_send = JSON.stringify(data);
    //console.log(data_to_send);
    //console.log(username, password);


    // 发送请求, 由响应结果判定用户是否可以login
    const xhr = new XMLHttpRequest(); // 它是一个用于发送请求的工具
    xhr.open("POST", database_server_url, true);
    //xhr.open("GET", database_server_url, true);
    xhr.setRequestHeader("Content-Type", "application/json"); // 为什么加上这一句之后就会发送失败

    xhr.onreadystatechange = function()
    {
        if (xhr.readyState === 4)
        {
            //console.log(1);
            // 已经完成了 HTTP 响应的接收, 即接收到了完整的响应报文
            if (xhr.status === 200)
            {
                //console.log(1);
                // 如果响应的状态码为2XX, 表示请求已成功被服务器接收、理解并接受。
                let response_body = JSON.parse(xhr.responseText);
                //console.log(response_body, typeof response_body);
                //console.log(response_body.rc, typeof response_body.rc);
                if (response_body.rc === "success")
                {
                    // 账号密码正确, 可以登陆
                    //console.log(response_body.rc);
                    // 如何在不使用全局变量的情况下, 实现跳转不同的页面, 不知道, 那就使用全局变量
                    if (data.usertype === "businessman")
                    {
                        // 跳转到./businessman_user_page.html页面
                        let userid = data.userid;
                        // 构造带有查询参数的URL
                        let url = "../businessman_user_page.html?userid=" + encodeURIComponent(userid);
                        // 跳转到新页面
                        window.location.href = url;
                        // 测试
                        //window.location.href = "./businessman_user_page.html";
                    }
                    else if (data.usertype === "customer")
                    {
                        // 跳转到./customer_user_page.html页面
                        // 用户的ID变量
                        let userid = data.userid;
                        // 构造带有查询参数的URL
                        let url = "./customer_user_page.html?userid=" + encodeURIComponent(userid);
                        // 跳转到新页面
                        window.location.href = url;
                        // 测试
                        //window.location.href = "./customer_user_page.html";
                    }
                    else if (data.usertype === "manufacturer")
                    {
                        // 跳转到./manufacturer_user_page.html页面
    
                        // 测试
                        window.location.href = "./manufacturer_user_page.html";
                        //window.location.href = "http://127.0.0.1:80/customer_user_page";
                    }
                    else 
                    {
                        // failure
                        // 其他是一个非法的usertype
                        //console.log(1);
                        // 跳转到./user_page.html页面 --------- 测试
                        window.location.href = "./user_page.html";
                        
                    }
                    
                }
                else
                {
                    //console.log(response_body);
                    // 账号密码不正确, 不可以登陆, 需要提示用户
                    //window.location.href = "user_page.html";
                    console.log("id or password error");
                }
            }
            else
            {

            }
        }
        
    }

    //console.log(data_to_send);
    xhr.send(data_to_send); // 请求体: 操作码, 用户类型, 用户账号, 用户密码
    //xhr.send();
}